/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author Enyths
 */
@ManagedBean
@RequestScoped
public class MusicsBean {
    private ArrayList<Music> tableMusic;

    public ArrayList<Music> getTableMusic() {
        return tableMusic;
    }
    
    /**
     * Creates a new instance of MusicsBean
     */
    public MusicsBean() {
        tableMusic = new ArrayList<Music>();
    }
    
    public void genererMusics(int idAuteur) {
        tableMusic.clear();
        
        try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("SELECT chanson.id, chanson.titre, chanson.texte, auteur.prenom, auteur.nom, chanson.auteurId FROM chanson, auteur where ? = chanson.auteurId and chanson.auteurId = auteur.id");
           requetePreparedMusic.setInt(1, idAuteur);
           ResultSet rset = requetePreparedMusic.executeQuery();
           while(rset.next()) {
        System.out.println(idAuteur);
               tableMusic.add(new Music(Integer.parseInt(rset.getString("id")), rset.getString("titre"), rset.getString("texte"), rset.getString("prenom"), rset.getString("nom"), Integer.parseInt(rset.getString("auteurId"))));
           }

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
}
